package com.gscitysfy.cus.drain.modules.command.convert;

import com.google.common.base.Splitter;
import com.gscitysfy.cus.drain.modules.command.dto.InstructTaskDto;
import com.gscitysfy.cus.drain.modules.command.entity.DangerSituationManage;
import com.gscitysfy.cus.drain.modules.command.entity.InstructCar;
import com.gscitysfy.cus.drain.modules.command.entity.InstructTeam;
import com.gscitysfy.cus.drain.modules.command.mapper.DangerSituationManageMapper;
import com.gscitysfy.cus.drain.utils.DateUtil;
import lombok.AllArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Author jiang_wen
 * @Date 2022/10/25 16:19
 * @Description TODO
 */
@AllArgsConstructor
@Component
public class InstructTaskConvert {

    private final DangerSituationManageMapper dangerSituationManageMapper;

    public InstructTaskDto toCarDto(InstructCar instructCar) {
        InstructTaskDto instructTaskDto = new InstructTaskDto();
        instructTaskDto.setCmKid(instructCar.getCmKid());
        instructTaskDto.setType("CAR");
        //发布人
        DangerSituationManage dangerSituationManage = dangerSituationManageMapper.selectById(instructCar.getDangerSituationId());
        if (Objects.nonNull(dangerSituationManage)) {
            instructTaskDto.setPublisher(dangerSituationManage.getCmCreator());
            instructTaskDto.setPondingDepth(dangerSituationManage.getPondingDepth());
            instructTaskDto.setAddress(dangerSituationManage.getAddress());
            //添加发布人手机号
            instructTaskDto.setPublisherPhone(dangerSituationManage.getPhone());
        }
        instructTaskDto.setPublishTime(Objects.nonNull(instructCar.getCmAddtime()) ? DateUtil.dateToDateString(instructCar.getCmAddtime(), "yyyy-MM-dd HH:mm:ss") : null);
        instructTaskDto.setStatus(instructCar.getStatus());
        instructTaskDto.setArriveFlag(instructCar.getArriveFlag());
        instructTaskDto.setArriveTime(Objects.nonNull(instructCar.getArriveTime()) ? DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneId.systemDefault()).format(instructCar.getArriveTime()) : null);

        instructTaskDto.setProblemDetail(instructCar.getProblemDetail());
        instructTaskDto.setFileId(instructCar.getFileId());
        instructTaskDto.setCloseFlag(instructCar.getCloseFlag());
        instructTaskDto.setSuspendFlag(instructCar.getSuspendFlag());
        //获取计时器时间
        String taskTimestamp = instructCar.getTaskTimestamp();
        if (StringUtils.isNotEmpty(taskTimestamp)) {
            List<String> taskTimestampList = new ArrayList<>(Splitter.on(",").splitToList(taskTimestamp));
            if (StringUtils.equals(instructCar.getStatus(), "ACCEPT")) {
                if (CollectionUtils.isNotEmpty(taskTimestampList)) {
                    if (!(taskTimestampList.size() % 2 == 0)) {
                        String now = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneId.systemDefault()).format(LocalDateTime.now());
                        taskTimestampList.add(now);
                    }
                    instructTaskDto.setTimer(timer(taskTimestampList));
                }
            }
        }
        return instructTaskDto;
    }

    public List<InstructTaskDto> toCarDtoList(List<InstructCar> instructCarList) {
        if (CollectionUtils.isEmpty(instructCarList)) {
            return Collections.emptyList();
        }
        return instructCarList.stream().map(this::toCarDto).collect(Collectors.toList());
    }


    public InstructTaskDto toTeamDto(InstructTeam instructTeam) {
        InstructTaskDto instructTaskDto = new InstructTaskDto();
        instructTaskDto.setCmKid(instructTeam.getCmKid());
        instructTaskDto.setType("TEAM");
        //发布人
        DangerSituationManage dangerSituationManage = dangerSituationManageMapper.selectById(instructTeam.getDangerSituationId());
        if (Objects.nonNull(dangerSituationManage)) {
            instructTaskDto.setPublisher(dangerSituationManage.getCmCreator());
            instructTaskDto.setPondingDepth(dangerSituationManage.getPondingDepth());
            instructTaskDto.setAddress(dangerSituationManage.getAddress());
            //添加发布人手机号
            instructTaskDto.setPublisherPhone(dangerSituationManage.getPhone());
        }
        instructTaskDto.setPublishTime(Objects.nonNull(instructTeam.getCmAddtime()) ? DateUtil.dateToDateString(instructTeam.getCmAddtime(), "yyyy-MM-dd HH:mm:ss") : null);
        instructTaskDto.setStatus(instructTeam.getStatus());
        instructTaskDto.setArriveFlag(instructTeam.getArriveFlag());
        instructTaskDto.setArriveTime(Objects.nonNull(instructTeam.getArriveTime()) ? DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneId.systemDefault()).format(instructTeam.getArriveTime()) : null);
        instructTaskDto.setTimer(instructTeam.getTimer());
        instructTaskDto.setProblemDetail(instructTeam.getProblemDetail());
        instructTaskDto.setFileId(instructTeam.getFileId());
        instructTaskDto.setCloseFlag(instructTeam.getCloseFlag());
        instructTaskDto.setSuspendFlag(instructTeam.getSuspendFlag());
        //获取计时器时间
        String taskTimestamp = instructTeam.getTaskTimestamp();
        if (StringUtils.isNotBlank(taskTimestamp)) {
            List<String> taskTimestampList = new ArrayList<>(Splitter.on(",").splitToList(taskTimestamp));
            if (StringUtils.equals(instructTeam.getStatus(), "ACCEPT")) {
                if (CollectionUtils.isNotEmpty(taskTimestampList)) {
                    if (!(taskTimestampList.size() % 2 == 0)) {
                        String now = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneId.systemDefault()).format(LocalDateTime.now());
                        taskTimestampList.add(now);
                    }
                    instructTaskDto.setTimer(timer(taskTimestampList));
                }
            }
        }
        return instructTaskDto;
    }

    public List<InstructTaskDto> toTeamDtoList(List<InstructTeam> instructTeamList) {
        if (CollectionUtils.isEmpty(instructTeamList)) {
            return Collections.emptyList();
        }
        return instructTeamList.stream().map(this::toTeamDto).collect(Collectors.toList());
    }

    public long timer(List<String> taskTimestampList) {
        long timer = 0;
        for (int i = 1; i <= taskTimestampList.size(); i++) {
            if (i % 2 == 0) {
                LocalDateTime end = DateUtil.parseLocalDateTime(taskTimestampList.get(i - 1));
                LocalDateTime star = DateUtil.parseLocalDateTime(taskTimestampList.get(i - 2));
                timer += Duration.between(star, end).toMillis();
            }
        }
        return timer / 1000;
    }
}
